/* Copyright (c) [Year] [name of copyright holder]
 * [Software Name] is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *      http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.gitee.empty_null.core.utils;

import java.util.Optional;
import java.util.function.Predicate;

/**
 * @author xuhainan
 * @date 2023/3/14 10:48
 * @region hefei
 */
public class OptionalUtil {

    public static <T> Optional<T> ofNullable(T value) {
        return value == null ? Optional.empty() : Optional.of(value);
    }

    public static <T> Optional<T> ofNullable(T value, Predicate<T> predicate) {
        return predicate.test(value) ? Optional.empty() : Optional.of(value);
    }

    public static String getString(String str1, String str2) {
        return getString(true, str1, str2);
    }

    public static String getString(boolean condition, String str1, String str2) {
        return getTarget(condition, str1, str2, String::isBlank);
    }

    public static Boolean getBoolean(Boolean str1, Boolean str2) {
        return getBoolean(true, str1, str2);
    }

    public static Boolean getBoolean(boolean condition, Boolean str1, Boolean str2) {
        return getTarget(condition, str1, str2, i -> true);
    }

    public static <T> T getTarget(T t1, T t2, Predicate<T> predicate) {
        return ofNullable(true, t1, predicate).orElse(t2);
    }

    public static <T> T getTarget(boolean condition, T t1, T t2, Predicate<T> predicate) {
        return ofNullable(condition, t1, predicate).orElse(t2);
    }

    public static <T> Optional<T> ofNullable(boolean condition, T value, Predicate<T> predicate) {
        return condition ? ofNullable(value, predicate) : Optional.empty();
    }


}
